<template>
  <div class="login">
    <div class="login-box">
      <img src="@/assets/common/login-logo.png" class="log" />
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item class="inp" prop="mobile">
          <el-input
            v-model="form.mobile"
            prefix-icon="el-icon-user-solid"
            placeholder="请输入账号"
          />
        </el-form-item>
        <el-form-item class="inp" prop="password">
          <el-input
            v-model="form.password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            show-password
            @keyup.enter.native="submit"
          />
        </el-form-item>
        <el-form-item>
          <el-button class="btn" :loading="loading" @click="submit">{{
            $t('login.login')
          }}</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { required: true, message: '必填信息', trigger: 'change' },
          {
            min: 11,
            max: 11,
            message: '请输入正确的手机号码',
            trigger: 'change'
          },
          {
            // 表单验证
            validator: (rules, value, callback) => {
              // 手机号码正则
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              // 正则验证 callback返回布尔值
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请正确输入手机号'))
              }
            },
            trigger: 'change'
          }
        ],
        password: [
          { required: true, message: '必填信息', trigger: 'change' },
          { min: 6, max: 12, message: '请输入正确的密码', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    submit () {
      // 全局表单验证
      this.$refs.form.validate(async result => {
        this.loading = true
        if (result) {
          try {
            await this.$store.dispatch('user/saveToken', this.form)
            this.loading = false
            const redirect = this.$route.query.redirect || '/'
            this.$router.push(redirect)
          } catch (error) {
            this.loading = false
          }
        } else {
          this.loading = false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
// @找的src只能在html,js中使用,~@找的src可以在css中使用
.login {
  width: 100%;
  height: 100%;
  background: url('~@/assets/common/login.jpg');
  display: flex;
  align-items: center;
  justify-content: center;
  .login-box {
    width: 436px;
    text-align: center;
    .inp {
      display: block;
      margin-bottom: 15px;
    }
    .log {
      display: block;
      margin-bottom: 20px;
    }
    .btn {
      width: 100%;
      color: #fff;
      background: #407ffe;
      border: 1px solid transparent;
    }
  }
}
</style>
